package com.sh.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.sh.common.core.enums.TransferDescEnum;
import com.sh.common.core.utils.MapstructUtils;
import com.sh.common.core.utils.StringUtils;
import com.sh.common.mybatis.core.page.TableDataInfo;
import com.sh.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sh.common.satoken.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sh.business.domain.bo.UserFundDetailBo;
import com.sh.business.domain.vo.UserFundDetailVo;
import com.sh.business.domain.UserFundDetail;
import com.sh.business.mapper.UserFundDetailMapper;
import com.sh.business.service.IUserFundDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户资产明细Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-08
 */
@RequiredArgsConstructor
@Service
public class UserFundDetailServiceImpl implements IUserFundDetailService {

    private final UserFundDetailMapper baseMapper;

    /**
     * 查询用户资产明细
     *
     * @param id 主键
     * @return 用户资产明细
     */
    @Override
    public UserFundDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询用户资产明细列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户资产明细分页列表
     */
    @Override
    public TableDataInfo<UserFundDetailVo> queryPageList(UserFundDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserFundDetail> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(UserFundDetail::getId);
        Page<UserFundDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        if (CollUtil.isNotEmpty(result.getRecords())) {
            result.getRecords().forEach(userFundDetailVo -> {
                userFundDetailVo.setDescribes(TransferDescEnum.of(Integer.valueOf(userFundDetailVo.getDescribes())).getDesc());
            });
        }
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<UserFundDetailVo> queryPortalPageList(UserFundDetailBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        LambdaQueryWrapper<UserFundDetail> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(UserFundDetail::getId);
        Page<UserFundDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户资产明细列表
     *
     * @param bo 查询条件
     * @return 用户资产明细列表
     */
    @Override
    public List<UserFundDetailVo> queryList(UserFundDetailBo bo) {
        LambdaQueryWrapper<UserFundDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserFundDetail> buildQueryWrapper(UserFundDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserFundDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, UserFundDetail::getUserId, bo.getUserId());
        lqw.in(CollUtil.isNotEmpty(bo.getDescribesList()), UserFundDetail::getDescribes, bo.getDescribesList());
        lqw.eq(bo.getType() != null, UserFundDetail::getType, bo.getType());
        lqw.eq(bo.getTransferFlag() != null, UserFundDetail::getTransferFlag, bo.getTransferFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), UserFundDetail::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getPointBefore() != null, UserFundDetail::getPointBefore, bo.getPointBefore());
        lqw.eq(bo.getAmount() != null, UserFundDetail::getAmount, bo.getAmount());
        lqw.eq(bo.getPointAfter() != null, UserFundDetail::getPointAfter, bo.getPointAfter());
        lqw.eq(StringUtils.isNotBlank(bo.getTxHash()), UserFundDetail::getTxHash, bo.getTxHash());
        lqw.eq(bo.getTxTime() != null, UserFundDetail::getTxTime, bo.getTxTime());
        lqw.like(StringUtils.isNotBlank(bo.getRemark()), UserFundDetail::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增用户资产明细
     *
     * @param bo 用户资产明细
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(UserFundDetailBo bo) {
        UserFundDetail add = MapstructUtils.convert(bo, UserFundDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户资产明细
     *
     * @param bo 用户资产明细
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(UserFundDetailBo bo) {
        UserFundDetail update = MapstructUtils.convert(bo, UserFundDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserFundDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除用户资产明细信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
